package ru.sushka.lunch.dao.files;

import java.text.ParseException;

import ru.sushka.lunch.enitiy.Customer;

/**
 * 10.10.12 : 22:26 User: sushka
 */
public class FileCustomerDao extends FileDao<Customer> {

    final static String FILE_NAME = "./db/customer.csv";

    @Override
    protected String getFileName() {
        return FILE_NAME;
    }

    @Override
    protected String convertToCsv(Customer persistent) {
        return persistent.getFirstname() + ";" + persistent.getLastname();
    }

    @Override
    protected Customer parseCsv(String csvline) throws ParseException {
        // validating
        if (csvline.trim().length() == 0) {
            throw new ParseException(csvline, 0);
        }
        String[] args = csvline.split(";");
        if (args.length != 2) {
            throw new ParseException(csvline, 0);
        }

        return new Customer(args[0].trim(), args[1].trim());
    }
}
